﻿using System;
using System.Collections.ObjectModel;
using System.IO;

namespace ZenWplExporter
{
	/// <summary>
	/// Represents Zen M3U Playlist
	/// </summary>
	internal class ZenPlayList
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ZenPlayList"/> class.
		/// </summary>
		public ZenPlayList()
		{
			Items = new Collection<string>();
		}

		/// <summary>
		/// Gets the items.
		/// </summary>
		/// <value>The items.</value>
		public Collection<string> Items { get; private set; }

		/// <summary>
		/// Saves the specified path.
		/// </summary>
		/// <param name="path">The path.</param>
		/// <param name="overwrite">if set to <c>true</c>, overwrites existing file.</param>
		public void Save(string path, bool overwrite)
		{
			if (File.Exists(path))
			{
				if (overwrite)
				{
					File.Delete(path);
				}
				else
				{
					throw new InvalidOperationException(String.Format("File [{0}] already exists", path));
				}
			}

			string[] items = new string[Items.Count];
			Items.CopyTo(items, 0);

			File.WriteAllLines(path, items);
		}
	}
}